/* * The MIT License
 *
 * Copyright (c) 2010 Dark Crimson, aka DCRM
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */


package jkiskeyblocker;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import java.io.File;
import java.util.List;
import javax.swing.JOptionPane;

public class MicroSpammer implements Runnable  {
    private String email;
    private String key;
    private Thread thread;
    private String code;
    private String host="https://activation.kaspersky.com/ru/";
    private String send_text="Получить по e-mail";

    public MicroSpammer(String key, String email) {
        this.key=key;
        this.email=email;
    }

    public void setSettings(String host, String send_text) {
        this.host=host;
        this.send_text=send_text;
    }

    public void init() {
        thread = new Thread(this);
        thread.setName("MicroSpammer");
        thread.start();
    }
    
    private void showCapthca() throws InterruptedException {
        CaptchaFrame frame = new CaptchaFrame();
        frame.getContentPane().add(new ImagePanel());
        frame.pack();
        frame.setVisible(true);
        while (frame.isVisible()) {Thread.sleep(100);}
        this.code=frame.getCode();
        frame.dispose();
    }

    public void run() {
        try {
            WebClient webClient = new WebClient();
            webClient.setJavaScriptEnabled(false);
            webClient.setCssEnabled(false);
            HtmlPage htmlPage0 = webClient.getPage(host);
            DomNodeList<HtmlElement> htmlImages =  htmlPage0.getElementsByTagName("img");
            HtmlImage htmlImage = (HtmlImage) htmlImages.get(3);
            htmlImage.saveAs(new File("./temp.png"));
            showCapthca();

            List<HtmlForm> htmlForm = htmlPage0.getForms();
            htmlForm.get(0).getInputByName("ACT_CODE").setValueAttribute(key);
            htmlForm.get(0).getInputByName("captcha").setValueAttribute(code);
            HtmlPage htmlPage1 = htmlForm.get(0).getInputByName("sendcode").click();

            List<HtmlForm> htmlForm1 = htmlPage1.getForms();
            htmlForm1.get(0).getInputByName("user_email").setValueAttribute(email);
            HtmlSubmitInput submitInput = htmlForm1.get(0).getInputByValue(send_text);
            for (int i=0;i<MainFrame.getMax();i++) {
                submitInput.click();
                MainFrame.incOK();
                Thread.sleep(100);
            }
            webClient.closeAllWindows();
            JOptionPane.showMessageDialog(null, "Spam OK->("+MainFrame.getMax()+") - "+email, "Spam - OK :)", JOptionPane.INFORMATION_MESSAGE);
            MainFrame.reset();
        } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Maximum Activation Reached!\nor Error Capthca!\n"+ex.getLocalizedMessage(), "Error or ...", JOptionPane.ERROR_MESSAGE); MainFrame.reset();}
    }

}
